package Group9.TodoManager.Controller;

import Group9.TodoManager.Data.Interface.ITableModel;
import Group9.TodoManager.Actions.AboutAction;
import Group9.TodoManager.Actions.CompleteAction;
import Group9.TodoManager.Actions.CreateTaskAction;
import Group9.TodoManager.Actions.IncompleteAction;
import Group9.TodoManager.Actions.LoadAction;
import Group9.TodoManager.Actions.TodoManagerAction;
import Group9.TodoManager.Actions.RemoveAction;
import Group9.TodoManager.Actions.EditAction;
import Group9.TodoManager.Actions.ExitAction;
import Group9.TodoManager.Actions.SelectAction;
import Group9.TodoManager.Data.NodeItem;
import Group9.TodoManager.DataStore.Repository;
import Group9.TodoManager.Settings;

import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ImageIcon;
import javax.swing.tree.TreeModel;

/**
 * This is a controller to handle operations on Task objects
 * @author volkan cambazoglu
 * @author Rupesh
 */
public class ITaskController<T> implements PropertyChangeListener {

    private ITableModel<T> tableModel;
    private Repository<T> repository;
    private TodoManagerAction loadAction, deleteAction, editAction,
            createAction, exitAction, selectAction, aboutAction,
            completeAction, incompleteAction;

    /**
     * Getter method of CompleteAction
     * @return completeAction instance
     */
    public TodoManagerAction getCompleteAction() {
		return completeAction;
	}
    
    /**
     * Getter method of IncompleteAction
     * @return incompleteAction instance
     */
    public TodoManagerAction getIncompleteAction() {
		return incompleteAction;
	}

	/**
     * Getter method of AboutAction
     * @return aboutAction instance
     */
    public TodoManagerAction getAboutAction() {
        return aboutAction;
    }

    /**
     * Setter method of AboutAction
     * @param aboutAction aboutAction instance
     */
    public void setAboutAction(TodoManagerAction aboutAction) {
        this.aboutAction = aboutAction;
    }

    /**
     * Getter method for select action.
     * @return returns the select action instance.
     */
    public TodoManagerAction getSelectAction() {
        return selectAction;
    }

    /**
     * getter method for exit action
     * @return returns the exit action instance.
     */
    public TodoManagerAction getExitAction() {
        return exitAction;
    }

    /**
     * This returns the model for tree that is used to show items hierarchically
     * @return treeModel
     */
    public TreeModel getTreeModel() {
        NodeItem item = new NodeItem(
        		Settings.getCurrentResourceBundle().getString("Tasks"), 
        		"Tasks");

        NodeItem alltasks = new NodeItem(
                Settings.getCurrentResourceBundle().getString("AllTasks"),
                "All Tasks");

        //NOW ADD THESE BELOW ALLTASKS NODE
        alltasks.addChildNode(new NodeItem(
                Settings.getCurrentResourceBundle().getString("PendingTasks"),
                "Pending Tasks"));
        alltasks.addChildNode(new NodeItem(
                Settings.getCurrentResourceBundle().getString("OverdueTasks"),
                "Overdue Tasks"));
        alltasks.addChildNode(new NodeItem(
                Settings.getCurrentResourceBundle().getString("FinishedTasks"),
                "Finished Tasks"));

        item.addChildNode(alltasks);

        NodeItem categoryFilter = new NodeItem(
                Settings.getCurrentResourceBundle().getString("FilterByCategory"),
                "Filter tasks by category");

        categoryFilter.addChildNode(new NodeItem(
        		Settings.getCurrentResourceBundle().getString("Category.Shopping"), 
        		"Shopping"));
        categoryFilter.addChildNode(new NodeItem(
        		Settings.getCurrentResourceBundle().getString("Category.Meeting"), 
        		"Meeting"));
        categoryFilter.addChildNode(new NodeItem(
        		Settings.getCurrentResourceBundle().getString("Category.Rent"), 
        		"Rent"));
        categoryFilter.addChildNode(new NodeItem(
        		Settings.getCurrentResourceBundle().getString("Category.Work"), 
        		"Work"));
        categoryFilter.addChildNode(new NodeItem(
        		Settings.getCurrentResourceBundle().getString("Category.Personal"), 
        		"Personal"));
        categoryFilter.addChildNode(new NodeItem(
        		Settings.getCurrentResourceBundle().getString("Category.Birthday"), 
        		"Birthday"));

        item.addChildNode(categoryFilter);

        return item;
    }

    /**
     * getter method for create action
     * @return returns the create action instance
     */
    public TodoManagerAction getCreateAction() {
        return createAction;
    }

    /**
     * getter method for edit action
     * @return returns the edit action instance
     */
    public TodoManagerAction getEditAction() {
        return editAction;
    }

    /**
     * getter method for load action
     * @return returns the load action instance
     */
    public TodoManagerAction getLoadAction() {
        return loadAction;
    }

    /**
     * getter method for remove action
     * @return returns the remove action instance
     */
    public TodoManagerAction getRemoveAction() {
        return deleteAction;
    }

    /**
     * getter method for table model
     * @return returns the table model instance
     */
    public ITableModel<T> getTableModel() {
        return tableModel;
    }

    /**
     * getter method for icon
     * @param iconpath the path of the icon
     * @return returns the image icon instance
     */
    public ImageIcon getIcon(String iconpath) {
        return new ImageIcon(getClass().getResource(iconpath));
    }

    /**
     * This is the controller class that provides the action instance for the view.
     * @param table table model
     * @param repo repository
     */
    public ITaskController(ITableModel<T> table, Repository<T> repo) {
        repository = repo;
        tableModel = table;

        //INITIALIZE THE ACTIONS
        createAction = new CreateTaskAction(
                Settings.getCurrentResourceBundle().getString("CreateAction"),
                getIcon("/Group9/TodoManager/Resource/Add.png"),
                KeyEvent.VK_C,
                Settings.getCurrentResourceBundle().getString("CreateAction"),
                Settings.getCreateTaskUI(null));

        loadAction = new LoadAction<T>(tableModel, repository, KeyEvent.VK_L);

        exitAction = new ExitAction<T>(
                Settings.getCurrentResourceBundle().getString("ExitAction"),
                getIcon("/Group9/TodoManager/Resource/Exit.png"),
                KeyEvent.VK_X,
                Settings.getCurrentResourceBundle().getString("ExitAction"));

        editAction = new EditAction<T>(
                Settings.getCurrentResourceBundle().getString("EditAction"),
                getIcon("/Group9/TodoManager/Resource/Edit.png"),
                Settings.getCurrentResourceBundle().getString("EditAction"),
                KeyEvent.VK_E,
                tableModel, repository);

        editAction.setEnabled(false);

        deleteAction = new RemoveAction<T>(
                Settings.getCurrentResourceBundle().getString("DeleteAction"),
                getIcon("/Group9/TodoManager/Resource/Delete.png"),
                Settings.getCurrentResourceBundle().getString("DeleteAction"),
                KeyEvent.VK_D,
                tableModel, repository);

        deleteAction.setEnabled(false);

        aboutAction = new AboutAction<T>(
                Settings.getCurrentResourceBundle().getString("AboutAction"),
                null, //getIcon("/Group9/TodoManager/Resource/Delete.png"),
                KeyEvent.VK_A,
                Settings.getCurrentResourceBundle().getString("AboutAction"),
                //tableModel,
                Settings.getAboutTaskUI());
        
        completeAction = new CompleteAction<T>(
        		Settings.getCurrentResourceBundle().getString("CompleteAction"), 
        		getIcon("/Group9/TodoManager/Resource/check.png"),
        		tableModel, repository);
        
        completeAction.setEnabled(false);
        
        incompleteAction = new IncompleteAction<T>(
        		Settings.getCurrentResourceBundle().getString("IncompleteAction"), 
        		getIcon("/Group9/TodoManager/Resource/Undo.gif"),
        		tableModel, repository);
        
        incompleteAction.setEnabled(false);

        selectAction = new SelectAction<T>(tableModel);
        tableModel.addPropertyChangeListener(this);
    }

    /**
     * This is the property change handler which change the state of UI
     * upon the selection change on the table view
     */
    public void propertyChange(PropertyChangeEvent evt) {        
        if (evt.getPropertyName().equalsIgnoreCase("SelectedRowIndex")) {
            int rowindex = Integer.parseInt(evt.getNewValue().toString());
            editAction.setEnabled(rowindex >= 0);
            deleteAction.setEnabled(rowindex >= 0);
            incompleteAction.setEnabled(rowindex >= 0);
        	completeAction.setEnabled(rowindex >= 0);
        }
    }
}